package com.zadetk.helpyou.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.OrdertomeBean;
import com.zadetk.helpyou.bean.ServiceItemBean;
import com.zadetk.helpyou.bean.SkilllistBean;
import com.zadetk.helpyou.net.BaseObserver;
import com.zadetk.helpyou.net.BaseResponse;
import com.zadetk.helpyou.net.NetTool;
import com.zadetk.helpyou.other.UserInfoManger;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.ToastUtil;

import java.sql.DatabaseMetaData;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 4-3-5发布我的服务页面
 */
public class MyServiceActivity extends AbsActivity implements View.OnClickListener {
    private static final String TAG = MyServiceActivity.class.getName();
    private TextView tvPublish;
    private RecyclerView recyclerView;
    private PublishServiceAdapter serviceAdapter;
    private List<SkilllistBean> data = new LinkedList<>();

    @Override
    public int setView() {
        return R.layout.activity_my_service;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
        requestData();
    }

    private void requestData() {


        getSkillList();

    }

    private void initLogic() {
        serviceAdapter = new PublishServiceAdapter(R.layout.publish_service_item, data );
        serviceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_edit:
                        startActivity(new Intent(MyServiceActivity.this, PublishServiceActivity.class));
                        break;
                    case R.id.iv_delete:
                        delete();
                        break;
                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(serviceAdapter);
        tvPublish.setOnClickListener(this);
    }

    private void delete() {
        ToastUtil.showToast("删除该条目！");
    }

    private void initViews() {
        tvPublish = findViewById(R.id.tv_btn_publish);
        recyclerView = findViewById(R.id.recycleview);
        setTopTitle("我的服务");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_publish:
                startActivity(new Intent(this, SearchItemActivity.class));
                break;
        }
    }

    private class PublishServiceAdapter extends BaseQuickAdapter<SkilllistBean, BaseViewHolder> {
        public PublishServiceAdapter(int layoutResId, @Nullable List<SkilllistBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SkilllistBean item) {
            helper.addOnClickListener(R.id.iv_edit);
            helper.addOnClickListener(R.id.iv_delete);
        }
    }
    private void getSkillList() {
        Map<String, Object> param = NetTool.newParams();
        param.put("uid", UserInfoManger.userInfo.getUid());
        param.put("token", UserInfoManger.userInfo.getUserToken());
        NetTool.getApi().getskilllist(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<SkilllistBean>>>() {

                    @Override
                    public void onData(BaseResponse<List<SkilllistBean>> value) {
                        if (value.getCode() == 0) {
                            data.clear();
                            List<SkilllistBean> user = value.getData();
                            data.addAll(user);
                            for (SkilllistBean bean : user) {
                                data.add(bean);
                                Log.i(TAG, "================="+bean);
                            }
                            serviceAdapter.notifyDataSetChanged();

                            LogUtil.e(user + " " + value.getMessage());
                        } else {
                            LogUtil.e(value.error_code + " error code");
                        }
                    }
                });
    }
}


